home *** CD-ROM | disk | FTP | other *** search
/ Univers Interactif 3 / INTERACTIF.BIN / mac / Planete.net / Internet Confirmés_Vrac / NetAgent.sea / src / subjects.c < prev    next >
C/C++ Source or Header  |  1992-06-15  |  8KB  |  287 lines

  1. /*
  2. This is an unsupported product. It was developed as part of a study at Ostfold
  3. Regional College, Norway in the spring of 1992.
  4.  
  5. You use this software at your own risk. Neither the authors nor the Ostfold
  6. Regional College nor any other person or organization except yourself is
  7. responsible for any damage or loss of data derived from the use of this
  8. software. You may distribute this software freely as long as it is in its
  9. original form and all of its files are included. If you make any changes to any
  10. part of this software or the files distributed with it, please state so and do
  11. NOT distribute it under its original name.
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <netdb.h>
  20. #include <fcntl.h>
  21. #include "nntp_tcp.h"
  22. #include "div_func.h"
  23.  
  24.  
  25. /*
  26. -----------------------------------------------------------------------------
  27. Function name: get_article()
  28. -----------------------------------------------------------------------------
  29. Parameters:    int f        A file descriptor
  30.         char *bf    A pointer to a buffer.
  31. Return value:    0 on error
  32.         1 on success
  33. Called by:    do_innlegg()
  34. Calls:        none
  35. Description:    This function gets the article id from a file. The id is
  36.         contained between a <>. It puts the id in bf.
  37. -----------------------------------------------------------------------------
  38. */
  39.  
  40. int get_article(f, bf)
  41. int f;
  42. char *bf;
  43. {
  44.     char ch[2];
  45.     int ret = 1;
  46.     int ret2 = 1;
  47.     int teller = 0;
  48.  
  49.     while(ret2 >0)
  50.     {    
  51.         ret2 = read(f, ch, 1);
  52.         if( ch[0] == '<' )
  53.         {
  54.             while(ret >0 )
  55.             {
  56.                 bf[teller++] = ch[0];    
  57.                 if(ch[0] == '>') {
  58.                     bf[teller] = '\0';
  59.                     return(1);
  60.                 }
  61.                 ret = read(f, ch, 1);
  62.             }
  63.             return(0);
  64.         }
  65.     }
  66.     return(0);
  67. }
  68.  
  69. /*
  70. -----------------------------------------------------------------------------
  71. Function name:    skriv()
  72. -----------------------------------------------------------------------------
  73. Parameters:    char *s        Pointer to a string.'
  74. Return value:    none
  75. Called by:    none
  76. Calls:        none
  77. Description:    This function prints the decimal value of each character
  78.         in a string. Used for debugging only.
  79. -----------------------------------------------------------------------------
  80. */
  81.  
  82. void skriv(s)
  83. char *s;
  84. {
  85.     int t=0;
  86.  
  87.     while(s[t] != '\0')
  88.     {
  89.         fprintf(stdout, " %3d", s[t++]);
  90.     }
  91. }
  92.  
  93. /*
  94. -----------------------------------------------------------------------------
  95. Function name: do_user()
  96. -----------------------------------------------------------------------------
  97. Parameters:    char *navn    Pointer to a string containing a username.
  98.         char *bf    Pointer to a buffer. The buffer contains
  99.                 the articles subject.
  100.         char *s        Pointer to a string which is the id and 
  101.                 subject of the article.
  102. Return value:    none
  103. Called by:    finn_stikkord()
  104. Calls:        lock_file(), unlock_file, les_linje(), contains()
  105. Description:    This function runs trough all of the lines in the file
  106.         <user>.subjects to see if any of them matches the one
  107.         in the bf buffer. If they match, the s string is written
  108.         to the file <user>.articles together with the line and
  109.         its priority.
  110. -----------------------------------------------------------------------------
  111. */
  112.  
  113. void do_user(navn, bf, s)
  114. char *navn;
  115. char *bf;
  116. char *s;
  117. {
  118.     int stikkord, innlegg, nfp;
  119.     char ord[BUFSIZ+1];
  120.     char ordfil[BUFSIZ+1], innleggfil[BUFSIZ+1];
  121.     char setn[BUFSIZ+1];
  122.     char newfile[BUFSIZ+1];
  123.     char *tmp;
  124.  
  125.     sprintf(ordfil, "%s/%s.subjects", navn, navn);
  126.     if((stikkord = open(ordfil, O_RDONLY|O_CREAT,  S_IREAD|S_IWRITE)) == -1) {
  127.                 fprintf(stderr, "%s: Couldn't open %s", myname, ordfil);
  128.             return;        
  129.     }
  130.     lock_file(stikkord);
  131.     while(les_linje(stikkord, ord)) {
  132.         tmp = ord + 1;
  133.         if(ord[0] != '0') {
  134.             if(contains(bf, tmp)) {
  135.                 sprintf(innleggfil, "%s/%s.articles", navn, navn);
  136.                 if((innlegg = open(innleggfil, O_WRONLY|O_APPEND|O_CREAT, S_IREAD|S_IWRITE)) == -1) {
  137.                                          fprintf(stderr, "%s: Couldn't open %s", myname, innleggfil);
  138.                                     return;    
  139.                         }
  140.                 lock_file(innlegg);
  141.                 sprintf(setn, "[%s] [%c] %s\n",tmp,ord[0], s);
  142.  
  143.                 write(innlegg, setn, strlen(setn));
  144.                 unlock_file(innlegg);
  145.                 close(innlegg);
  146.  
  147.                 sprintf(newfile, "%s/%s.newnews", navn, navn);
  148.                 if((nfp = open(newfile, O_WRONLY|O_TRUNC|O_CREAT, S_IREAD|S_IWRITE)) == -1) {
  149.                                        fprintf(stderr, "%s: Couldn't open %s",myname, innleggfil);
  150.                                     return;    
  151.                                    }
  152.                 lock_file(nfp);
  153.                 write(nfp, "YES\n", 4);
  154.                 unlock_file(nfp);
  155.                 close(nfp);
  156.                 unlock_file(stikkord);
  157.                 close(stikkord);
  158.                 return;
  159.             }
  160.         }    
  161.     }
  162.     unlock_file(stikkord);
  163.     close(stikkord);
  164. }
  165.  
  166. /*
  167. -----------------------------------------------------------------------------
  168. Function name: finn_stikkord() 
  169. -----------------------------------------------------------------------------
  170. Parameters:    char *bf        Contains the subject of an article
  171.         char *s            Contains the id and subject of an
  172.                     article.
  173. Return value:    none
  174. Called by:    print_subject()
  175. Calls:        lock_file(), unlock_file(), les_linje(), first_word(),
  176.         do_user()
  177. Description:    This function runs trough all the users and checks for each
  178.         of them if they want to read the article.
  179. -----------------------------------------------------------------------------
  180. */
  181.  
  182. void finn_stikkord(bf, s)
  183. char *bf;
  184. char *s;
  185. {
  186.     int brukere;
  187.     char navn[BUFSIZ+1];
  188.     
  189.     if((brukere = open("agent.users", O_RDONLY|O_CREAT,  S_IREAD|S_IWRITE)) == -1) {
  190.         fprintf(stderr, "%s: Couldn't open agent.users", myname);
  191.         return;    
  192.     }
  193.     lock_file(brukere);
  194.     while(les_linje(brukere, navn)) {
  195.         first_word(navn);
  196.         do_user(navn, bf, s);
  197.     }
  198.     unlock_file(brukere);
  199.     close(brukere);
  200.  
  201. }
  202.  
  203. /*
  204. -----------------------------------------------------------------------------
  205. Function name: print_subject()
  206. -----------------------------------------------------------------------------
  207. Parameters:    char *s        Contains the id and subject of an article.
  208. Return value:    none
  209. Called by:    do_innlegg()
  210. Calls:        finn_stikkord()
  211. Description:    Get the subject from the string s and sends both the subject
  212.         and s to finn_stikkord().
  213. -----------------------------------------------------------------------------
  214. */
  215.  
  216. void print_subject(s)
  217. char *s;
  218. {
  219.     int teller = 0;
  220.     int t2 = 0;
  221.     char bf[BUFSIZ+1];
  222.  
  223.     while(s[teller++] != 32);
  224.  
  225.     teller--;
  226.  
  227.     while(s[teller] != '\0') 
  228.         bf[t2++] = s[teller++];
  229.  
  230.     bf[t2] = '\0';
  231.  
  232.     finn_stikkord(bf, s);
  233. }
  234.  
  235. /*
  236. -----------------------------------------------------------------------------
  237. Function name: do_innlegg()
  238. -----------------------------------------------------------------------------
  239. Parameters:    int s        A valid socket.
  240. Return value:    none
  241. Called by:    main() (agentc)
  242. Calls:        lock_file(), unlock_file(), get_article(), tcp_send()
  243.         get_msg(), print_subject()
  244. Description:    Reads every article id in New.News and gets the subject
  245.         for that article from the NNTP-server(using the XHDR-
  246.         command). The format recieved is: <id> subject. It passes
  247.         the sring recieved to the print_subject().
  248. -----------------------------------------------------------------------------
  249. */
  250.  
  251. void do_innlegg(s)
  252. int s;
  253. {
  254.     int f;
  255.     int l;
  256.     int test;
  257.     char command[80];
  258.     char article[80];
  259.     char *host;
  260.     char *afile;
  261.     char buf[BUFSIZ+1];
  262.  
  263.     test  = 1;
  264.  
  265.     if((f = open( "New.News", O_RDONLY)) == -1) {
  266.         fprintf(stderr, "%s: Couldn't open New.News", myname);
  267.                 return;
  268.     }
  269.  
  270.     lock_file(f);
  271.     test = get_article(f, article);
  272.  
  273.     while(test) {
  274.         sprintf( command, "xhdr subject %s\n", article);
  275.         tcp_send(s, command);
  276.         if(get_msg(s, '2', LINEFEED, buf, 3) == 1) {
  277.             if(get_msg(s, '<', LINEFEED, buf, 3) == 1)    {
  278.                 print_subject(buf);
  279.                 get_msg(s, '2', LINEFEED, buf, 3);
  280.             }
  281.         }
  282.         test = get_article(f, article);
  283.     }
  284.     unlock_file(f);
  285.     close(f);
  286. }
  287.